
"""
    pyprosim - a simple prolog compiler developed to PCS2056 course (Compilers) at Poli-USP\
    Copyright (C) 2008 Rodrigo Rodrigues da Silva

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
    
"""

from pyprosim.token import *
from pycoopiler.enum import enum
from pyprosim.submaquina import submaquina

class maq_args(submaquina):
    
    states = enum("_0_INICIAL _1_FINAL _2 _3")
    
    def __init__(self, _tkzr):
        self.tkzr = _tkzr
        self.state = self.states._0_INICIAL
        self.log_chamada("ARGS")
        self.current = self.tkzr.current
        
    def analyze(self):
           
        while not (self.state == self.states._1_FINAL and self.current.type != self.current.types.VIRGULA):  
            self.log_state()
            
            if self.state == self.states._0_INICIAL:
                if self.current.type == self.current.types.INF:
                    self.state = self.states._1_FINAL
                elif self.current.type == self.current.types.NUM or self.current.type == self.current.types.NOME:
                    self.state = self.states._3
                else: raise Exception ("Erro Sintatico! Esperando INF, NOME ou NUM")
                self.consume()
            
            elif self.state == self.states._1_FINAL:
                if self.current.type == self.current.types.VIRGULA:
                    self.state = self.states._2
                else: raise Exception ("Erro Sintatico! Esperando VIRGULA")
                self.consume()
            
            elif self.state == self.states._2:
                if self.current.type == self.current.types.NUM or self.current.type == self.current.types.NOME:
                    self.state = self.states._3
                else: raise Exception ("Erro Sintatico! Esperando NOME ou NUM")
                self.consume()
            
            elif self.state == self.states._3:
                if self.current.type == self.current.types.VIRGULA:
                    self.state = self.states._2
                else: raise Exception ("Erro Sintatico! Esperando VIRGULA")
                self.consume()
            
            else: raise Exception("ERRO: Estado invalido: " + self.state)
        
        self.log_retorno("ARGS")